﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Markup;
using System.Linq.Dynamic;

namespace UIT.Controls.Data
{
    [ContentProperty("Collection")]
    public class EnumerableDataProvider : IDataProvider, IDisposable
    {
        public EnumerableDataProvider(IEnumerable enumerable)
        {
            this.Enumerable = enumerable;
            CanInsert = false;
            CanUpdate = false;
            CanDelete = false;
        }

        public EnumerableDataProvider(ICollection<object> enumerable)
        {
            this.Enumerable = enumerable;
        }

        public EnumerableDataProvider()
        {
            Enumerable = new List<object>();
        }

#if !SILVERLIGHT
        public ICollection<object> Collection { get { return (ICollection<object>)Enumerable; } set { Enumerable = value; } }
#else
        public IList Collection { get { return (IList)Enumerable; } set { Enumerable = value; } }
#endif

        public IEnumerable Enumerable { get; set; }

        #region IDataProvider Members

        public string Id
        {
            get;
            set;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion

        #region IDataProvider Members


        public bool CanSelect
        {
            get { return true; }
        }

        public bool CanInsert
        {
            get;
            private set;
        }

        public bool CanDelete
        {
            get;
            private set;
        }

        public bool CanUpdate
        {
            get;
            private set;
        }

        #endregion


        public IEnumerable Select(SelectArgument arg)
        {
            if (Enumerable == null)
                return System.Linq.Enumerable.Empty<object>();
            var queryable = Enumerable.AsQueryable();
            
            if (arg.RequestedOperations.HasFlag(Operations.Sort) && !string.IsNullOrEmpty(arg.SortExpression))
                queryable = queryable.OrderBy(arg.SortExpression);
            
            if (arg.RequestedOperations.HasFlag(Operations.Page))
            {
                arg.TotalRowCount = queryable.Count();
                queryable = queryable.Skip(arg.StartRowIndex).Take(arg.MaximumRows);
            }
            return queryable;
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        public void Insert()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public IControl Parent
        {
            get;
            set;
        }

        #region IDataProvider Members


        public Operations SupportedOperations
        {
            get
            {
                return Operations.Select | Operations.Page;
            }
        }

        #endregion
    }

    internal class EmptyEnumerator : IEnumerator
    {
        #region IEnumerator Members

        public object Current
        {
            get { return null; }
        }

        public bool MoveNext()
        {
            return false;
        }

        public void Reset()
        {
        }

        #endregion
    }
}
